package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.goods.RefundGoodsJson;
import com.drinks.yiniang.entity.user.order.OrderConfirmJson;
import com.drinks.yiniang.entity.user.order.OrderCouponJson;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

/**
 * 选择优惠券列表 adapter
 *
 * Created by han on 2019/6/27 0027.
 */

public class CouponSelectorAdapter extends BaseAdapter {
    private Context mContext;

    private LayoutInflater inflater;

    private List<OrderCouponJson.OrderCouponData> mList;
    private OrderCouponJson.OrderCouponData mData;

    private Drawable mCheckDrawable;
    private Drawable mNormalDrawable;

    private StringBuilder mBuilder;

    public CouponSelectorAdapter(Context mContext, List<OrderCouponJson.OrderCouponData> mList) {
        this.mContext = mContext;
        this.mList = mList;

        inflater = LayoutInflater.from(mContext);

        mCheckDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_choose_p);
        mNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_choose_n);
    }

    @Override
    public int getCount() {
        return CommonUtils.getListSize(mList);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;

        if (convertView == null) {
            mHolder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_coupon_selector, null);

            mHolder.tv_coupon_s_name = convertView.findViewById(R.id.tv_coupon_s_name);
            mHolder.tv_coupon_s_limit = convertView.findViewById(R.id.tv_coupon_s_limit);

            mHolder.iv_coupon_s_choose = convertView.findViewById(R.id.iv_coupon_s_choose);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        mData = mList.get(position);

        //name
        mBuilder = new StringBuilder();
        mBuilder.append(StringUtils.getNullEmptyConvert__(mData.getAmount()));
        mBuilder.append("元");
        mBuilder.append(StringUtils.getNoNullString(mData.getName()));
        mHolder.tv_coupon_s_name.setText(mBuilder);

        //满多少使用
        mHolder.tv_coupon_s_limit.setText(String.format(mContext.getString(R.string.text_use_limit_money_format), StringUtils.getNullEmptyConvert__(mData.getLimit_amount())));

        if(mData.isChecked()) {
            mHolder.iv_coupon_s_choose.setImageDrawable(mCheckDrawable);
        } else {
            mHolder.iv_coupon_s_choose.setImageDrawable(mNormalDrawable);
        }

        return convertView;
    }

    class ViewHolder {
        //name
        TextView tv_coupon_s_name;
        TextView tv_coupon_s_limit;
        //选中
        ImageView iv_coupon_s_choose;
    }
}
